package com.asia.algorithmcode.array;


/**
 * @Num: 704. 二分查找
 * @Addr: https://leetcode.cn/problems/binary-search/description/
 * @Type: easy
 * @Author: wanfu
 * @Date: 2024/7/15 星期一 12:00
 * @Desc: 经典的左闭右闭的区间查找是十分经典的，需要注意的是左开右闭或者左闭右开的区间查找是最需要注意的，往往需要注意区间收缩是偏向的元素是否是希望比较的元素
 */
class Solution_search {


    public int search(int[] nums, int target) {
        int n = nums.length;
        int left = 0;
        int right = n - 1;

        while (left <= right) {
            int mid = left + (right - left) / 2;

            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] >= target) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }

        }
        return -1;
    }


    public static void main(String[] args) {
        int[] arr = new int[]{-1,0,3,5,9,12};
        System.out.println(new Solution_search().search(arr, 9));
    }
}
